package cn.iocoder.yudao.module.pi.service.bizlog.retry;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pi.api.BizApiLogSearchClient;
import cn.iocoder.yudao.module.pi.controller.admin.bizlog.retry.dto.ApiInterfaceRetryDTO;
import cn.iocoder.yudao.module.pi.controller.admin.bizlog.retry.dto.ApiInterfaceRetryPageDTO;
import cn.iocoder.yudao.module.pi.controller.admin.bizlog.retry.vo.ApiInterfaceRetryVO;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static cn.iocoder.yudao.module.pi.enums.LogRecordConstants.*;

/**
 * <p>
 * 接口重试记录表 服务实现类
 * </p>
 *
 * @author shaoxh
 * @since 2025-06-27
 */
@Service
public class ApiInterfaceRetryServiceImpl implements ApiInterfaceRetryService {

    @Resource
    private BizApiLogSearchClient bizApiLogSearchClient;

    @Override
    @LogRecord(type = API_RETRY, subType = API_RETRY_EDIT_SUB_TYPE, bizNo = "0", success = API_RETRY_EDIT_DATA_SUCCESS)
    public CommonResult<String> updateJsonData(ApiInterfaceRetryDTO request) {
        String responseStr = bizApiLogSearchClient.post(JSONUtil.toJsonStr(request),
                System.getenv("RETRY") + "/admin-api/cl/data-exchange/apiInterfaceRetry/update_jsonData");
        LogRecordContext.putVariable("requestId", request.getRequestId());
        if (StrUtil.isNotBlank(responseStr)) {
            return JSONUtil.toBean(responseStr, new TypeReference<CommonResult<String>>() {
            }, false);
        }
        return CommonResult.success(null);
    }

    @Override
    @LogRecord(type = API_RETRY, subType = API_RETRY_SEND_SUB_TYPE, bizNo = "0", success = API_RETRY_SEND_SUCCESS)
    public CommonResult<String> resend(String requestId) {
        String responseStr = bizApiLogSearchClient.get(System.getenv("RETRY") + "/admin-api/cl/data-exchange/apiInterfaceRetry/resend/" + requestId);
        LogRecordContext.putVariable("requestId", requestId);
        if (StrUtil.isNotBlank(responseStr)) {
            return JSONUtil.toBean(responseStr, new TypeReference<CommonResult<String>>() {
            }, false);
        }
        return CommonResult.success(null);
    }

    @Override
    @LogRecord(type = API_RETRY, subType = API_RETRY_QUERY_SUB_TYPE, bizNo = "0", success = API_RETRY_QUERY_SUCCESS)
    public CommonResult<PageResult<ApiInterfaceRetryVO>> pageData(ApiInterfaceRetryPageDTO request) {
        String responseStr = bizApiLogSearchClient.post(JSONUtil.toJsonStr(request),
                System.getenv("RETRY") + "/admin-api/cl/data-exchange/apiInterfaceRetry/page");
        if (StrUtil.isNotBlank(responseStr)) {
            return JSONUtil.toBean(responseStr, new TypeReference<CommonResult<PageResult<ApiInterfaceRetryVO>>>() {
            }, false);
        }
        return CommonResult.success(new PageResult<>(0L));
    }
}
